package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerMainTabComponent;
import com.ewzj.bigdata.di.module.MainTabModule;
import com.ewzj.bigdata.mvp.contract.MainTabContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.TabBean;
import com.ewzj.bigdata.mvp.presenter.MainTabPresenter;
import com.ewzj.bigdata.mvp.ui.fragment.CollectFragment;
import com.ewzj.bigdata.mvp.ui.fragment.MineFragment;
import com.ewzj.bigdata.mvp.ui.fragment.RecommendFragment;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.jess.arms.base.AdapterViewPager;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class MainTabActivity extends BaseActivity<MainTabPresenter> implements MainTabContract.View {


    @BindView(R.id.bnve)
    BottomNavigationViewEx bnve;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private long exitTime = 0;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerMainTabComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mainTabModule(new MainTabModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_maintab;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initTab();
    }
    public void initTab(){
        List<Fragment> fragments=new ArrayList<>();
        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);
        bnve.setTextVisibility(true);
        fragments.add(CollectFragment.newInstance());
        fragments.add(RecommendFragment.newInstance());
        fragments.add(MineFragment.newInstance());
        FragmentManager fragmentManager=getSupportFragmentManager();
        AdapterViewPager adapterViewPager=new AdapterViewPager(fragmentManager,fragments);
        vpContent.setAdapter(adapterViewPager);
        bnve.setupWithViewPager(vpContent);
    }
    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit(){
        if ((System.currentTimeMillis() - exitTime) > AppConstants.EXIT_DURATION) {
            ArmsUtils.makeText(this,"再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            ArmsUtils.exitApp();
        }
    }

    @Override
    public void configFragments(List<TabBean> tabBeans) {

    }
}
